﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.ComponentModel;


using System.Drawing;
using System.Web.SessionState;

namespace PlanInfo
{
    public partial class OsnovniInfomacii : System.Web.UI.Page
    {
        protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        protected System.Data.SqlClient.SqlConnection Connection1;
        protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        protected System.Data.SqlClient.SqlDataReader sqldr;
     
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void OdiNadokumentacijaButton_Click(object sender, EventArgs e)
        {            
            string conn = "Data Source=ELENA-PC\\SQLEXPRESS;Initial Catalog=BusinessPlanDB;Integrated Security=True";
            SqlConnection Connection1 = new SqlConnection(conn);
            Connection1.Open();
           
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd1 = new SqlCommand();
            SqlCommand cmd2 = new SqlCommand();
            SqlCommand cmd3 = new SqlCommand();

            cmd.Connection = Connection1;
            cmd1.Connection = Connection1;
            cmd2.Connection = Connection1;
            cmd3.Connection = Connection1;
            /*
            cmd1.CommandText = "Insert into Projects(projectName,projectStart,projectOwner,projectPeriod) values ('" + ImeNaPlanotTextBox.Text.ToString().ToLower() + "','" + PocetokNaPlanot.SelectedDate.ToString().ToLower() + "','" + AvtorNaPlanotTextBox.Text.ToString().ToLower() + "','" + PeriodDropDownList.SelectedItem.ToString().ToLower() + "')";
            cmd1.ExecuteNonQuery();
            Connection1.Close();
             string _userName = Membership.GetUser(User.Identity.Name.ToLower());
            */
            //userName = Membership.GetUser(User.Identity.Name.ToLower());
            MembershipUser username = Membership.GetUser(User.Identity.Name.ToLower());
            cmd.CommandText = " Select count(ownerID) from Projects where projectOwner = '" + username + " ' ";
            SqlDataReader sqldr = cmd.ExecuteReader();
            sqldr.Read();
            if (sqldr[0].ToString() == "0")
            {  
                Connection1.Close();

                Connection1.Open();
                cmd1.CommandText = "Insert into Projects(projectOwner) values ('" + username + "')";
                cmd1.ExecuteNonQuery();
                Connection1.Close();

                Connection1.Open();
                cmd2.CommandText = " Select ownerID from Projects where projectOwner = '" + username + "' ";
                SqlDataReader sqldr1 = cmd2.ExecuteReader();
                sqldr1.Read();
                int id = Convert.ToInt32(sqldr1[0].ToString());
                sqldr1.Close();
                Connection1.Close();

                Connection1.Open();
                cmd3.CommandText = "Insert into Documentation(projectOwner,ownerID,projectName,projectStart,projectPeriod) values ('" + username + "','" + id + "','" + ImeNaPlanotTextBox.Text.ToString().ToLower() + "','" + PocetokNaPlanot.SelectedDate.ToString().ToLower() + "','" + PeriodDropDownList.SelectedItem.ToString().ToLower() + "')";
                cmd3.ExecuteNonQuery();
                Connection1.Close();
               }
                else
                {
                    if (sqldr[0].ToString() == "1")
                    {
                        Connection1.Close();
                        Connection1.Open(); //AvtorNaPlanotTextBox.Text.ToString().ToLower()
                        cmd2.CommandText = " Select ownerID from Projects where projectOwner = '" + username + "' ";
                        SqlDataReader sqldr1 = cmd2.ExecuteReader();
                        sqldr1.Read();
                        int id = Convert.ToInt32(sqldr1[0].ToString());

                        Connection1.Close();

                        Connection1.Open();
                        cmd3.CommandText = "Insert into Documentation(projectOwner,ownerID,projectName,projectStart,projectPeriod) values ('" + username + "','" + id + "','" + ImeNaPlanotTextBox.Text.ToString().ToLower() + "','" + PocetokNaPlanot.SelectedDate.ToString().ToLower() + "','" + PeriodDropDownList.SelectedItem.ToString().ToLower() + "')";
                        cmd3.ExecuteNonQuery();
                        Connection1.Close();
                     }
                 } 
                sqldr.Close();
                string projectName = ImeNaPlanotTextBox.Text;
            Response.Redirect("Dokumentacija/KratokPregled.aspx?projectName=" + projectName);
             
            
        }
    }
}
